﻿package com.onlinesys.action;

import com.onlinesys.beans.UserInfo;
import com.onlinesys.service.LoginService;
import com.onlinesys.service.impl.LoginServiceImpl;
import com.onlinesys.utils.Des;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

    private static final long serialVersionUID = 1L;

    String userName = null;

    String password = null;

    String checkCode = null;

    String msg = null;

    public String execute() throws Exception {
        if (userName == null || "".equals(userName.trim())) {
            msg = "usernameEmpty";
            return ERROR;
        } else if (userName.length() > 20) {
            msg = "usernameMoreThan20";
            return ERROR;
        } else if (password == null || "".equals(password)) {
            msg = "passwordEmpty";
            return ERROR;
        } else if (password.length() > 20) {
            msg = "passwordMoreThan20";
            return ERROR;
        }

        LoginService loginService = new LoginServiceImpl();
        Des des = new Des();
        String encryptPassword = des.encrypt(password);
        UserInfo ui = loginService.login(userName, encryptPassword);
        if (ui.getUserId() != null) {
            ActionContext.getContext().getSession().put("user", ui);
            msg = "success";
        } else {
            msg = "invalidAccount";
            return ERROR;
        }
        return SUCCESS;
    }

    public String getMsg() {
        return msg;
    }

    public String getCheckCode() {
        return checkCode;
    }

    public void setCheckCode(String checkCode) {
        this.checkCode = checkCode;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

}